﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MINERP.MODEL.Statistics;
using MINERP.DAL;

namespace MINERP.BLL.InfoConfig
{
    public  class SalesmanBLL
    {
        /// <summary>
        /// 获得业务员信息列表
        /// </summary>
        /// <returns></returns>
        public List<Salesman_Info> GetSalesmanInfoList()
        {
            List<Salesman_Info> salesInfoList = new List<Salesman_Info>();

            using (newSampleYPDBEntities enty = new newSampleYPDBEntities())
            {
                List<SalesmanBalance> sales = enty.SalesmanBalance.ToList();
                foreach (var item in sales)
                {
                    Salesman_Info salesmanInfo = new Salesman_Info()
                    {
                        Balance = item.Balance,
                        Remark = item.Remark,
                        SalesmanId = item.SalesmanId,
                        SalesmanName = item.SalesmanName,
                        Tel1 = item.Tel1,
                        Address = item.Address,
                        Describe = item.Describe,
                        Fax = item.Fax,
                        RequestPaperName = item.RequestPaperName
                    }; 
                    salesInfoList.Add(salesmanInfo);
                }
            }
            return salesInfoList;
        }

        /// <summary>
        /// 获得业务员信息
        /// </summary>
        /// <returns></returns>
        public Salesman_Info GetSalesmanInfo(string salesmanName)
        {
            Salesman_Info salesmanInfo = null;
            using (newSampleYPDBEntities enty = new newSampleYPDBEntities())
            {
                SalesmanBalance saleman = enty.SalesmanBalance.FirstOrDefault(s => s.SalesmanName == salesmanName);

                salesmanInfo = new Salesman_Info()
                {
                    Balance = saleman.Balance,
                    Remark = saleman.Remark,
                    SalesmanId = saleman.SalesmanId,
                    SalesmanName = saleman.SalesmanName,
                    Tel1 = saleman.Tel1,
                    Address = saleman.Address,
                    Describe = saleman.Describe,
                    Fax = saleman.Fax,
                    RequestPaperName = saleman.RequestPaperName
                };
            }
            return salesmanInfo;
        }


        /// <summary>
        /// 添加业务员
        /// </summary>
        /// <param name="salesmanInfo"></param>
        /// <returns>0，成功 1，数据库中已存在该名称的业务员 2，异常</returns>
        public int Insert(Salesman_Info salesmanInfo)
        {
            int ret = 0;
            try
            {
                using (newSampleYPDBEntities db = new newSampleYPDBEntities())
                {
                    SalesmanBalance salesmanIndb = db.SalesmanBalance.FirstOrDefault(s => s.SalesmanName == salesmanInfo.SalesmanName);
                    if (salesmanIndb == null)
                    {
                        SalesmanBalance salesman = new SalesmanBalance()
                        {
                            Balance = 0,
                            SalesmanName = salesmanInfo.SalesmanName,
                            Remark = salesmanInfo.Remark,
                            Tel1 = salesmanInfo.Tel1,
                            Address = salesmanInfo.Address,
                            Describe = salesmanInfo.Describe,
                            Fax = salesmanInfo.Fax,
                            RequestPaperName = salesmanInfo.RequestPaperName

                        }; 
                        db.AddToSalesmanBalance(salesman);
                        db.SaveChanges();
                    }
                    else
                    {
                        ret = 1;
                    }
                }
            }
            catch
            {
                ret = 2;
            }

            return ret;
        }

        public int Update(Salesman_Info salesmanInfo)
        {
            int ret = 0;
            try
            {
                using (newSampleYPDBEntities db = new newSampleYPDBEntities())
                {
                    SalesmanBalance salesman = db.SalesmanBalance.FirstOrDefault(s=>s.SalesmanId ==salesmanInfo.SalesmanId);
                    SalesmanBalance salesmanupdate = db.SalesmanBalance.FirstOrDefault(s => s.SalesmanName == salesmanInfo.SalesmanName);
                    if (salesmanupdate == null || salesman.SalesmanId == salesmanupdate.SalesmanId)
                    {
                        salesman.Remark = salesmanInfo.Remark;
                        salesman.SalesmanName = salesmanInfo.SalesmanName;
                        salesman.Tel1 = salesmanInfo.Tel1;
                        salesman.Address = salesmanInfo.Address;
                        salesman.Describe = salesmanInfo.Describe;
                        salesman.Fax = salesmanInfo.Fax;
                        salesman.RequestPaperName = salesmanInfo.RequestPaperName;
       
                        db.SaveChanges();
                    }
                    else
                    {
                        ret = 1;
                    }

                }
            }
            catch
            {
                ret = 2;
            }
            return ret;
        }
    }
}
